import { NextRequest, NextResponse } from 'next/server'
import { prisma } from '@/lib/prisma'

// GET /api/orders/:id - 获取单个订单
export async function GET(
    request: NextRequest,
    { params }: { params: { id: string } }
) {
    try {
        const id = parseInt(params.id)

        if (isNaN(id)) {
            return NextResponse.json(
                { success: false, error: 'Invalid ID' },
                { status: 400 }
            )
        }

        const order = await prisma.order.findUnique({
            where: { id },
            include: { customer: true, course: true }
        })

        if (!order) {
            return NextResponse.json(
                { success: false, error: 'Order not found' },
                { status: 404 }
            )
        }

        return NextResponse.json({
            success: true,
            data: order
        })
    } catch (error) {
        console.error('Error fetching order:', error)
        return NextResponse.json(
            { success: false, error: 'Failed to fetch order' },
            { status: 500 }
        )
    }
}

// PUT /api/orders/:id - 更新订单
export async function PUT(
    request: NextRequest,
    { params }: { params: { id: string } }
) {
    try {
        const id = parseInt(params.id)

        if (isNaN(id)) {
            return NextResponse.json(
                { success: false, error: 'Invalid ID' },
                { status: 400 }
            )
        }

        const body = await request.json()
        const { status, course_id, customer_id } = body

        const updateData: any = {}
        if (status !== undefined) updateData.status = status
        if (course_id) updateData.course_id = parseInt(course_id)
        if (customer_id) updateData.customer_id = customer_id

        const order = await prisma.order.update({
            where: { id },
            data: updateData,
            include: { customer: true, course: true }
        })

        return NextResponse.json({
            success: true,
            data: order
        })
    } catch (error: any) {
        console.error('Error updating order:', error)

        if (error.code === 'P2025') {
            return NextResponse.json(
                { success: false, error: 'Order not found' },
                { status: 404 }
            )
        }

        if (error.code === 'P2003') {
            return NextResponse.json(
                { success: false, error: 'Invalid course_id or customer_id' },
                { status: 400 }
            )
        }

        return NextResponse.json(
            { success: false, error: 'Failed to update order' },
            { status: 500 }
        )
    }
}

// DELETE /api/orders/:id - 删除订单
export async function DELETE(
    request: NextRequest,
    { params }: { params: { id: string } }
) {
    try {
        const id = parseInt(params.id)

        if (isNaN(id)) {
            return NextResponse.json(
                { success: false, error: 'Invalid ID' },
                { status: 400 }
            )
        }

        await prisma.order.delete({
            where: { id }
        })

        return NextResponse.json({
            success: true,
            message: 'Order deleted successfully'
        })
    } catch (error: any) {
        console.error('Error deleting order:', error)

        if (error.code === 'P2025') {
            return NextResponse.json(
                { success: false, error: 'Order not found' },
                { status: 404 }
            )
        }

        return NextResponse.json(
            { success: false, error: 'Failed to delete order' },
            { status: 500 }
        )
    }
}
